<?php
if(Request::post("time")){
	$today=strtotime (Request::post("times")); //今天
	$past=strtotime (Request::post("time")); //2015-5-20
	$dif=ceil(($today-$past)/86400); //60s*60min*24h
	die($dif);
}
$mu = Session::get('mu');
$ua = new Model('user');
$ua->find($_GET["id"]);
$ma = new Model('user_agency');
$ma->find($mu->id);

// $mp = new Model('user_prices');
// $mp->find(array('isadmin'=>1));
// if($mp->id){
// 	$prc_arr = json_decode($mp->price,true);
// 	unset($prc_arr['1']);
// }else{
// 	$prc_arr = array('2'=>2400,'3'=>4800,'4'=>7200,'5'=>9600,'6'=>12000,'7'=>12000,'8'=>12000);
// }

// $mp->find(array('uid'=>$mu->id));
// $mp = new Model('user_setmeal');
// $mp->find(array('user_agency_id'=>$ma->id));
$mp = get_user_setmeal($ma->id);

$prc_arrs = json_decode($mp->price,true);
// if($mp->id){
// 	$prc_arrs = json_decode($mp->price,true);
// 	unset($prc_arrs['1']);
// }else{
// 	$mp->find(array('isadmin'=>1));
// 	if($mp->id){
// 		$prc_arrs = json_decode($mp->price,true);
// 		unset($prc_arrs['1']);
// 	}else{
// 		$prc_arrs = array('2'=>2400,'3'=>4800,'4'=>7200,'5'=>9600,'6'=>12000,'7'=>12000,'8'=>12000);
// 	}
// }
if(\Request::post("price_id")){
	echo $prc_arrs[\Request::post("price_id")];
	die;
}
$m = new SampleModel();
$yhlx_arr = translate_level();
// unset($yhlx_arr['1']);
if(!$mu->isyg){
	$mp2 = new Model('agency_price');
	$prc_arr2 = $mp2->map_array('id', 'price');
	$zsjiagebiao = intval($prc_arr2[$mu->level])/10;
}else{
	$zsjiagebiao = 10;
}
$ys = array();
for($i=0;$i<37;$i++){
	$ys[$i] = $i;
}

if(strtotime($ua->mendtime) < time()){
	$ua->mendtime = date("Y-m-d",time());
}
if(myStrtotime($ua->mendtime)>time() && $ua->level_id!=1 ){
	$day_price=$prc_arrs[$ua->level_id]/365;//当前会员日价格
	$day_time=floor((myStrtotime($ua->mendtime)-time())/86400);//当前会员剩余天数（省略小数）
	$return_price=$day_time*$day_price;//当前会员折扣金钱数
	$check=1;
}else{
	$check=0;
	$day_price=0;
	$day_time=0;
	$return_price=0;
}
if(Request::post('xfDays')){
	$xfDays = Request::post('xfDays');//续费天数
	$hf = Request::post('sjPrice');//实际花费
	$lx = Request::post('lx');//续费类型
	//进行充值
	$red = new Model('agency_pay_record');
	$red->uid = $ua->id;
	$red->pid = $ma->id;
	$red->money = $hf;
	$red->months = $xfDays;
	$red->level_id = $lx;
	$red->cwid = $ua->cwid;
	$red->un = $ua->un;
	$red->bz = Request::post('bz');
	$red->btime = DB::raw('now()');
	$red->isyg = $mu->isyg;
	if($red->save()){
		$ma->yue = intval($ma->yue)-$hf;
		$ua->level_id=$red->level_id;
		$ua->mendtime= formatDateByTimeStamp(myStrtotime($ua->mendtime?$ua->mendtime:'',$red->months));
		$ma->save();
		$ua->save();	
	}
	Response::write('ok');
}

//根据给定的时间戳格式化为给定的时间
function formatDateByTimeStamp($timeStamp){
	$datetime = new DateTime("@{$timeStamp}");
	$datetime->setTimezone(new DateTimeZone('Asia/Shanghai'));
	return $datetime->format('Y-m-d H:i:s');
}

function myStrtotime($nowTime='',$ad=0,$type='D'){
	$datetime = new \DateTime($nowTime);
	$interval = new \DateInterval("P{$ad}{$type}");
	$datetime->add($interval);
    return $datetime->format('U');
}

//获取代理用户套餐, 如果当前代理没权限, 往上级代理找
function get_user_setmeal($user_agency_id){
	$setmeal = (new Model('user_setmeal'))->find(['user_agency_id'=>$user_agency_id]);
	if($setmeal->id){
		return $setmeal;
	}
	$user_agency = (new Model('user_agency'))->find($user_agency_id);
	$setmeal = get_user_setmeal($user_agency->pid);
	return $setmeal;
}